package com.baseproject.util;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.view.View;

import com.baseproject.BaseApplication;

import java.util.List;


/**
 * Created by hp on 2017/3/28.
 */

public class DeviceUtils {

    /**
     * heapSize是设备分配给app的最大堆内存   maxHeapSize 是当配置了android:largeHeap="true" 才有的最大堆内存，一般是heapSize的2-3倍
     */
    private void printHeapInfo() {
        ActivityManager manager = (ActivityManager) BaseApplication.getApplication().getSystemService(Context.ACTIVITY_SERVICE);
        int heapSize = manager.getMemoryClass();
        int maxHeapSize = manager.getLargeMemoryClass();  // manafest.xml   android:largeHeap="true"
        LogUtils.d("heapSize = " + heapSize + " maxHeapSize = " + maxHeapSize);
    }

    /**
     * 获得屏幕高度
     * @param context 上下文对象
     * @return 屏幕的宽度
     */
    public static int getScreenWidth(Context context) {
        int width = 0;
        if (null != context) {
            width = context.getResources().getDisplayMetrics().widthPixels;
        } else {
            width = BaseApplication.getApplication().getResources().getDisplayMetrics().widthPixels;
        }
        return width;
    }

    public static int getScreenWidth() {
        return getScreenWidth(null);
    }

    /**
     * 获得屏幕宽度
     *
     * @param context 上下文对象
     * @return 屏幕的高度
     */
    public static int getScreenHeight(Context context) {
        int height = 0;
        if (null != context) {
            height = context.getResources().getDisplayMetrics().heightPixels;
        } else {
            height = BaseApplication.getApplication().getResources().getDisplayMetrics().heightPixels;
        }
        return height;
    }

    public static int getScreenHeight() {
        return getScreenHeight(null);
    }

    /**
     * 获得状态栏的高度
     *
     * @param context 上下文对象
     * @return 状态栏高度
     */
    public static int getStatusHeight(Context context) {
        int statusHeight = -1;
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
            statusHeight = context.getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusHeight;
    }

    /**
     * 根据手机密度动态的设置间距
     *
     * @param context 上下文对象
     * @param space   间距
     * @return 乘以密度后的间距
     */
    public static float getSpSpace(Context context, float space) {
        if (null != context) {
            space = space * context.getResources().getDisplayMetrics().scaledDensity;
        }
        return space;
    }

    /**
     * 获取当前屏幕截图，包含状态栏
     *
     * @param activity 上下文
     * @return 屏幕截图(包含状态栏)
     */
    public static Bitmap snapShotWithStatusBar(Activity activity) {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bmp = view.getDrawingCache();
        int width = getScreenWidth(activity);
        int height = getScreenHeight(activity);
        Bitmap bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
        view.destroyDrawingCache();
        return bp;
    }

    /**
     * 获取当前屏幕截图，不包含状态栏
     *
     * @param activity 上下文
     * @return 屏幕截图(不包含状态栏)
     */
    public static Bitmap snapShotWithoutStatusBar(Activity activity) {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bmp = view.getDrawingCache();
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;

        int width = getScreenWidth(activity);
        int height = getScreenHeight(activity);
        Bitmap bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight);
        view.destroyDrawingCache();
        return bp;
    }

    /**
     * 判断手机中是否安装了指定包
     *
     * @return
     */
    public static boolean checkIfPackageInstalled(Context context, String packageName) {
        List<PackageInfo> packages = context.getPackageManager().getInstalledPackages(0);
        PackageInfo packageInfo;
        boolean isInstall = false;
        for (int i = 0; i < packages.size(); i++) {
            packageInfo = packages.get(i);
            if (null != packageInfo && packageName.equals(packageInfo.packageName)) {
                isInstall = true;
                return isInstall;
            }
        }
        return isInstall;
    }

    public static Activity getActivityUsingView(View view) {
        Context context = view.getContext();
        return getActivityUsingContext(context);
    }

    public static Activity getActivityUsingContext(Context context) {
        if (context instanceof Activity) {
            Activity activity = (Activity) context;
            return activity;
        }
        return null;
    }

    public static DisplayMetrics getDisplayMetricsUsingContext(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        Activity activity = getActivityUsingContext(context);
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm;
    }

    /* Checks if external storage is available for read and write */
    public static boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        return false;
    }

    /* Checks if external storage is available to at least read */
    public static boolean isExternalStorageReadable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state) ||
                Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            return true;
        }
        return false;
    }
}
